﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlanningGIS.Oracle;
using PlanningGIS.MapSheet;
using System.IO;
using PlanningGIS.Util.IO;
using PlanningGIS.Util;
using PlanningGIS.ArcGIS.Custom;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Serialization;
using PlanningGIS.ArcGIS;
using PlanningGIS.Framework;
using PlanningGIS.Framework.Config;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.Util.Data;
using PlanningGIS.Util.Net;
using PlanningGIS.MetaData;

namespace PlanningGIS.Product
{
    /// <summary>
    /// 产品管理类
    /// </summary>
    public class ProductHelper
    {   
        /// <summary>
        /// 获取产品配置信息存放的路径
        /// </summary>
        public static string ProductPath
        {
            get { return AppConfig.SysFolder + "\\Product"; }
        }

        /// <summary>
        /// 元数据规则文件路径
        /// </summary>
        public static readonly string MetaRulePath = ProductHelper.ProductPath + "\\MetaRule.xml";

        /// <summary>
        /// SQL查询时需要过滤掉的字段名称
        /// </summary>
        public static readonly List<string> NoQueryFields = new List<string> { "Shape" };

        /// <summary>
        /// 需要过滤调的字段
        /// </summary>
        public static readonly List<string> FilterFields = new List<string> { "Shape_Area", "Shape_Len", "Shape.Area", "Shape.Len", "Shape_Length", "Shape.Length" };

        /// <summary>
        /// 不能为空的字段
        /// </summary>
        public static readonly List<string> NoNullFields = new List<string> { "图号", "测区代号", "比例尺分母", "产品生产日期" };

        /// <summary>
        /// 字段中不能出现的字符串
        /// </summary>
        public static readonly List<string> NoHasString = new List<string> { "-", "、" };

        /// <summary>
        /// 字段中包含了字符后不需要显示的字符
        /// </summary>
        public static readonly List<string> VisibleString = new List<string> { "ID", "ObjectID", "序号", "分发" };

        /// <summary>
        /// 投影文件
        /// </summary>
        public static string PrjFullName
        {
            get { return AppConfig.CoordinatePath + "\\" + ProductConfig.LasSpatialName; }
        }

        /// <summary>
        /// 产品配置
        /// </summary>
        private static ProductConfig _ProductConfig = null;
        
        /// <summary>
        /// 产品配置
        /// </summary>
        public static ProductConfig ProductConfig
        {
            get
            {
                if (_ProductConfig == null)
                {
                    try
                    {
                        _ProductConfig = ConfigHelper.GetObject<ProductConfig>();
                        if (_ProductConfig.ProType != zjgisProType.FTP && !string.IsNullOrWhiteSpace(_ProductConfig.DataViewPath))
                        {
                            if (!Directory.Exists(_ProductConfig.DataViewPath))
                                Directory.CreateDirectory(_ProductConfig.DataViewPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        MsgHelper.ShowErrorMsgbox("产品文件配置【ProductConfig】异常：" + ex.Message);
                    }
                }
                return _ProductConfig;
            }
        }
        
        /// <summary>
        /// 元数据数据表规则信息
        /// </summary>
        private static List<MetaRule> _MetaRules = null;

        /// <summary>
        /// 元数据数据表规则信息
        /// </summary>
        public static List<MetaRule> MetaRules
        {
            get
            {
                if (_MetaRules == null)
                {
                    if (File.Exists(MetaRulePath))
                        _MetaRules = (List<MetaRule>)XmlSerialization.Deserialize<List<MetaRule>>(MetaRulePath, null);
                    else _MetaRules = new List<MetaRule>();
                }
                return _MetaRules;
            }
        }

        /// <summary>
        /// 产品数据库配置
        /// </summary>
        private static DbConfig _DbConnect = null;

        /// <summary>
        /// 获取产品数据库配置
        /// </summary>
        public static DbConfig DbConnect
        {
            get
            {
                if (_DbConnect == null)
                {
                    _DbConnect = new DbConfig(MetaHelper.FrameConfig.ConnectName);
                }
                return _DbConnect;
            }
        }

        /// <summary>
        /// Ftp操作对象
        /// </summary>
        private static FTPRequest _ftp = null;

        /// <summary>
        /// 获取Ftp操作对象
        /// </summary>
        public static FTPRequest Ftp
        {
            get
            {
                if (_ftp == null)
                {
                    string uri =string.Format("ftp://{0}/",AppConfig.SystemConfig.FTP.Server);
                    if(AppConfig.SystemConfig.FTP.Port!=21)
                        uri = string.Format("ftp://{0}:{1}/", AppConfig.SystemConfig.FTP.Server, AppConfig.SystemConfig.FTP.Port);
                    _ftp = new FTPRequest(new Uri(uri), AppConfig.SystemConfig.FTP.UserName, AppConfig.SystemConfig.FTP.Password);
                }
                return _ftp;
            }
            set { _ftp = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private static string _FtpPath = "";
        /// <summary>
        /// ftp访问路径
        /// </summary>
        public static string FtpPath
        {
            get
            {
                //string localPath = AppConfig.SystemConfig.FTP.LocalPath;
                //if (!Directory.Exists(localPath))
                //{
                //    string remotePath = @"\\" + AppConfig.SystemConfig.FTP.Server + "\\" + AppConfig.SystemConfig.FTP.ShareDir;
                //    string username = AppConfig.SystemConfig.FTP.UserName;
                //    string password = AppConfig.SystemConfig.FTP.Password;
                //    int rst = NetFileService.Connect(remotePath, localPath, username, password, false);
                //    if (rst != 0) return "";
                //}
                //return localPath;
                if (string.IsNullOrWhiteSpace(_FtpPath))
                {
                    if (AppConfig.SystemConfig.FTP.Server == "127.0.0.1" && Directory.Exists(AppConfig.SystemConfig.FTP.LocalPath)) 
                    {
                        _FtpPath = AppConfig.SystemConfig.FTP.LocalPath;
                    }
                    else
                    {
                        string s = string.Format(@"\\{0}\{1}", AppConfig.SystemConfig.FTP.Server, AppConfig.SystemConfig.FTP.ShareDir);
                        //连接共享文件夹
                        bool status = PlanningGIS.Util.Net.FileShare.connectState(s, AppConfig.SystemConfig.FTP.UserName, AppConfig.SystemConfig.FTP.Password);
                        if (status) _FtpPath = s;
                    }                    
                }
                return _FtpPath;
            }
        }

    }
}
